﻿using AnyCAD.Foundation;

namespace Rapid.Sketch.Plugin.Polygon
{
    /// <summary>
    /// 定义多边形数据
    /// </summary>
    class MyPolygonModel : ElementModel
    {
        public GPntList Points = new GPntList();
        public MyPolygonModel()
        {

        }

        public TopoShape CreateShape()
        {
            if (Points.Count < 3)
                return null;
            return SketchBuilder.MakePolygon(Points, true);
        }

        public void CreateElement(Document doc)
        {
            var schema = ElementSchemaManager.Instance().FindSchema(nameof(MyPolygonModel));
            var properties = schema.CreateComponent("Rect", doc);
            Save(properties);
        }
    }

    /// <summary>
    /// 定义多边形的行为
    /// </summary>
    class MyPolygonSchema : ElementSchema
    {
        public MyPolygonSchema()
        {
            SetName(nameof(MyPolygonModel));
            SetModifier(nameof(MyPolygonModel));

            SetIcon("/Rapid.Common.Res;component/Image/SectionBar.png");
        }

        public override ElementModel CreateModel()
        {
            return new MyPolygonModel();
        }

        public override string OnGetEntityClassName()
        {
            return nameof(ShapeElement);
        }

        public override bool OnParameterChanged(Document document, SchemaComponent properties)
        {
            var pEntity = ShapeElement.Cast(properties.GetEntity());
            if (pEntity == null)
                return false;

            var model = new MyPolygonModel();
            model.Load(properties);
            var shape = model.CreateShape();
            if (shape == null)
                return false;

            pEntity.SetShape(shape);
            return true;
        }
    }

    /// <summary>
    /// 实现对多边形的修改器
    /// </summary>
    class MyPolygonModifier : DrawableModifier
    {
        public MyPolygonModifier()
            : base(nameof(MyPolygonModel))
        {
        }

        /// <summary>
        /// 收集编辑夹点
        /// </summary>
        /// <param name="pElement"></param>
        /// <param name="collector"></param>
        public override void CollectGrips(DrawableElement pElement, GripCollector collector)
        {
            var properties = SchemaComponent.Cast(pElement.GetComponent(nameof(SchemaComponent)));
            if (properties == null)
                return;
            var trf = pElement.GetTransform();

            var model = new MyPolygonModel();
            model.Load(properties);
            uint ii = 0;
            foreach (var pt in model.Points)
            {
                var node = new GripNode(pElement.GetId(), ii++, EnumGripType.Vertex, Vector3d.From(pt.Transformed(trf)));
                collector.Add(node);
            }
        }

        /// <summary>
        /// 根据夹点修改几何对象
        /// </summary>
        /// <param name="pElement"></param>
        /// <param name="grip"></param>
        /// <returns></returns>
        public override bool ChangeByGrip(DrawableElement pElement, GripNode grip)
        {
            var properties = SchemaComponent.Cast(pElement.GetComponent(nameof(SchemaComponent)));
            if (properties == null)
                return false;

            var model = new MyPolygonModel();
            model.Load(properties);
            if (grip.GetGripId() >= model.Points.Count)
                return false;

            var trf = pElement.GetTransform();
            
            model.Points[(int)grip.GetGripId()] = grip.GetPoint().ToPnt().Transformed(trf.Inverted());
            model.Save(properties);

            return true;
        }
    }
}
